package nku.csc594.project1;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import java.text.DecimalFormat;

import nku.csc594.R;

public class PayCalculatorActivity extends Activity {
	 	Button btnCalc, btnClear;
	    EditText etHours, etPayRate, etTotalPay, etFedWH, etStateWH, etNetPay;
		private static final Float fedTaxRate = new Float(.22);
		private static final Float stateTaxRate = new Float(.022);
		private static final Float overTimePayRate = new Float(1.5);
		
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
	        
	        //Sets Buttons
	        btnCalc = (Button)findViewById(R.id.button1);
	        btnClear = (Button)findViewById(R.id.button2);
	        
	        //Sets EditTextView's
	        etHours = (EditText)findViewById(R.id.hours);
	        etPayRate = (EditText)findViewById(R.id.payRate);
	        etTotalPay = (EditText)findViewById(R.id.totalPay);
	        etFedWH = (EditText)findViewById(R.id.fedWH);
	        etStateWH = (EditText)findViewById(R.id.stateWH);
	        etNetPay = (EditText)findViewById(R.id.netPay);
	    }
	    //Does calculations for the 4 output text views... 
	    public void doCalculate(View view)
	    {
	    	if(checkForErrors() == false){	
	    		// Parsing Input
	    		Float hours = Float.parseFloat(etHours.getText().toString());
	    		Float payRate = Float.parseFloat(etPayRate.getText().toString());
	    		
	    		//Function Calls to calculate values
	    		Float totalPay = getTotalPay(hours, payRate);
	    		Float fedWH = getFederalTaxRate(totalPay);
	    		Float stateWH = getStateTaxRate(totalPay);
	    		
	    		//In case we add more deductions.. .
	    		Float deductions[] = new Float[2];
	    		deductions[0] = fedWH;
	    		deductions[1] = stateWH;
	    		
	    		//Ending total net calculation
	    		Float totalNet = this.getNetPay(totalPay, this.getTotalDeductions(deductions));
	    		
	    		//Format for output... I prefer using this since HOURS isn't a currency value.. but customer doesn't want this... 
	    		//NumberFormat currency = NumberFormat.getCurrencyInstance();
	    		
	    		//DecimalFormat for two decimals places for the output text views...  
	    		DecimalFormat decimalFormat = new DecimalFormat(".00");
	    		
	    		//Output
	    		etTotalPay.setText(decimalFormat.format(totalPay));
	    		etFedWH.setText(decimalFormat.format(fedWH));
	    		etStateWH.setText(decimalFormat.format(stateWH));
	    		etNetPay.setText(decimalFormat.format(totalNet));
	    	}
	    }
	    
	    //Checks for all errors including for negative numbers but i couldn't find a way to enter in negative numbers on the emulator... 
	    public Boolean checkForErrors(){
	    	if(etHours.getText().toString().isEmpty() || etPayRate.getText().toString().isEmpty())
	    	{
	    		//Checks to see if both inputs are blank
	    		if(etHours.getText().toString().isEmpty() && etPayRate.getText().toString().isEmpty()){
		    		new AlertDialog.Builder(this)
		    		  .setTitle("No Entry in Hours and Pay Rate")
		    		  .setMessage("You must make an entry in both hours and pay rate to do a calculation.")
		    		  .setNeutralButton("OK", null)
		    		  .show();
		    		return true;
	    		}
	    		//Checks to see if hour is blank
	    		else if(etHours.getText().toString().isEmpty()){
		    		new AlertDialog.Builder(this)
		    		  .setTitle("No Entry in Hours")
		    		  .setMessage("You must make an entry in hours to do a calculation.")
		    		  .setNeutralButton("OK", null)
		    		  .show();
		    		return true;
		    		
	    		}
	    		//Checks to see if pay rate is blank
	    		else if(etPayRate.getText().toString().isEmpty()){
		    		new AlertDialog.Builder(this)
		    		  .setTitle("No Entry in Pay Rate")
		    		  .setMessage("You must make an entry in pay rate to do a calculation.")
		    		  .setNeutralButton("OK", null)
		    		  .show();
		    		return true;
	    		}
	    		//Checks for any other unexpected errors like they somehow entered in a alpha even though that doesn't seem possible.. 
	    		else{
	    			new AlertDialog.Builder(this)
		    		  .setTitle("Something is wrong!")
		    		  .setMessage("Something went horribly wrong, shut this app down now.")
		    		  .setNeutralButton("OK", null)
		    		  .show();
	    			return true;
	    		}
	    	}
	    	else{
	    		//Checks if somehow they entered in a negative number, i tried this in the emulator and it wasn't possible. 
	    		if(Float.parseFloat(etHours.getText().toString()) < 0 || Float.parseFloat(etPayRate.getText().toString()) < 0){
	    			new AlertDialog.Builder(this)
		    		  .setTitle("Positive Numbers")
		    		  .setMessage("You must enter positive values for both hours and pay rate.")
		    		  .setNeutralButton("OK", null)
		    		  .show();
	  			return true;
	    		}
	    		return false;
	    	}
	    }
	    
	    //gets total pay give hours and payRate.. and calculates overtime if over 40 hours.. 
	    public Float getTotalPay(Float hours, Float payRate){
	    	Float totalPay; 
	    	if(hours > 40){
				totalPay = 40 * payRate;
				totalPay += (hours - 40) * payRate * overTimePayRate;
			}
			else{
				totalPay = hours * payRate;
			}
	    	return totalPay;
	    }
	    
	    // Gets federal tax rate using your gross pay
	    public Float getFederalTaxRate(Float totalPay){
	    	Float fedWH = (float) (totalPay * fedTaxRate);
	    	return fedWH;
	    }
	    
	    //Gets state tax rate using your gross pay
	    public Float getStateTaxRate(Float totalPay){
	    	Float stateWH = (float) (totalPay * stateTaxRate);
	    	return stateWH;
	    }
	    
	    //Calculates total of deductions.. added this in case we have more deductions
	    public Float getTotalDeductions(Float... deductions){
	    	Float totalDeductions = new Float(0); 
	    	for(Float deduction : deductions){
	    		totalDeductions += deduction;
	    	}
	    	return totalDeductions;
	    }
	    
	    //Calculates net pay using gross pay minus array of deductions
	    public Float getNetPay(Float totalPay, Float deductions){
	    	Float totalNet = totalPay - deductions;
	    	return totalNet;
	    }
	    
	    //For clear button if its clicked clears all values and puts(REQUESTS) the focus on hours
	    public void doClear(View view)
	    {
	        etHours.setText("");
	        etPayRate.setText("");
	        etTotalPay.setText("");
	        etFedWH.setText("");
	        etStateWH.setText("");
	        etNetPay.setText("");
	        etHours.requestFocus();
	    }
	}